package com.menker.app.common;

import android.graphics.Color;
import android.text.TextUtils;

import com.menker.app.R;

import java.util.List;

import rx.functions.Action0;

/**
 * @author J.Huang
 * @version 1.0
 */
public class ToolbarOwner {

    private IToolbar iToolbar;
    private Config config;

    public ToolbarOwner(IToolbar iToolbar) {
        this.iToolbar = iToolbar;
    }

    private void update() {
        iToolbar.setToolbarTitle(config.title);
        iToolbar.setDisplayShowHomeEnabled(config.showHomeEnabled);
        if (config.upButtonEnabled) {
            iToolbar.setUpButtonEnabled(true);
            // 设置图标的操作和upButtonEnabled函数的效果会相互覆盖
            iToolbar.setNavigationIcon(config.navigationIcon);
        } else {
            iToolbar.setUpButtonEnabled(false);
        }

        iToolbar.setMenu(config.menuActionList);
        iToolbar.setToolbarVisible(config.visible);
        iToolbar.setToolbarTitleColor(config.titleColor);
        iToolbar.setToolbarBgColor(config.bgColor);
    }

    public Config getConfig() {
        return config;
    }

    public void setConfig(Config config) {
        this.config = config;
        update();
    }

    public void updateMenu(List<MenuAction> menuActionList) {
        this.config.menuActionList = menuActionList;
        iToolbar.setMenu(config.menuActionList);
    }

    public interface IToolbar {
        void setToolbarTitle(CharSequence title);

        /***
         * 使左上角程序图标是否显示，如果设成false，则没有程序图标，仅仅就个标题，否则，显示应用程序图标
         * 对应id为android.R.id.home，对应ActionBar.DISPLAY_SHOW_HOME
         * @param enabled
         */
        void setDisplayShowHomeEnabled(boolean enabled);
        /***
         *  给左上角图标的左边加上一个返回的图标。对应ActionBar.DISPLAY_HOME_AS_UP
         * @param enabled
         */
        void setUpButtonEnabled(boolean enabled);

        void setNavigationIcon(int resId);

        void setMenu(List<MenuAction> menuActionList);

        void setToolbarVisible(boolean visible);

        void setToolbarTitleColor(int titleColor);
        void setToolbarBgColor(int bgColor);
    }

    public static class Config {
        // 使左上角图标是否显示，如果设成false，则没有程序图标，仅仅就个标题，否则，显示应用程序图标
        public final boolean showHomeEnabled;
        public final boolean upButtonEnabled;
        public final CharSequence title;
        public final int navigationIcon;
        public List<MenuAction> menuActionList;
        public final boolean visible;
        public final int titleColor;
        public final int bgColor;

        public Config(CharSequence title, List<MenuAction> menuActionList) {
            this(title, true, menuActionList, 0, 0);
        }

        public Config(CharSequence title, boolean upButtonEnabled, List<MenuAction> menuActionList) {
            this(title, upButtonEnabled, menuActionList, 0, 0);
        }

        public Config(CharSequence title, boolean upButtonEnabled, List<MenuAction> menuActionList,
                      int titleColor, int bgColor) {
            this.visible = true;
            this.title = title;
            this.showHomeEnabled = false;
            this.upButtonEnabled = upButtonEnabled;
            this.navigationIcon = R.drawable.ic_back;
            this.menuActionList = menuActionList;
            this.titleColor = titleColor;
            this.bgColor = bgColor;
        }

        public Config(CharSequence title, boolean upButtonEnabled, List<MenuAction> menuActionList,
                      String titleHexColor, String bgHexColor) {
            this.visible = true;
            this.title = title;
            this.showHomeEnabled = false;
            this.upButtonEnabled = upButtonEnabled;
            this.navigationIcon = R.drawable.ic_back;
            this.menuActionList = menuActionList;
            if (TextUtils.isEmpty(Utils.Color.getColorString(titleHexColor))) {
                this.titleColor = 0;
            } else {
                this.titleColor = Color.parseColor(Utils.Color.getColorString(titleHexColor));
            }
            if (TextUtils.isEmpty(Utils.Color.getColorString(bgHexColor))) {
                this.bgColor = 0;
            } else {
                this.bgColor = Color.parseColor(Utils.Color.getColorString(bgHexColor));
            }
        }

        public Config(boolean visible) {
            this.visible = false;
            this.title = "";
            this.showHomeEnabled = false;
            this.upButtonEnabled = false;
            this.navigationIcon = 0;
            this.menuActionList = null;
            this.titleColor = 0;
            this.bgColor = 0;
        }
    }

    public static class MenuAction {
        public final CharSequence title;
        public final Action0 action;
        public final int icon;

        public MenuAction(CharSequence title, Action0 action, int icon) {
            this.title = title;
            this.action = action;
            this.icon = icon;
        }
    }
}
